package com.he.ymw.controller;

import com.he.ymw.pojo.Order;
import com.he.ymw.pojo.PaymentBO;
import com.he.ymw.pojo.User;
import com.he.ymw.service.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

/**
 * 处理订单的控制器
 */
@Controller
public class OrderController {

    @Resource
    OrderService orderService;


    /**
     * 结算
     * @param cost 所有商品的总价格
     * @param proIds 每样商品的id
     * @param quantitys 每样商品的数量
     * @param costs 每样商品的总价格
     */
    @RequestMapping("/settlement")
    public String settlement(@RequestParam("cost") Integer cost,
                             @RequestParam("proIds") String proIds,
                             @RequestParam("quantitys") String quantitys,
                             @RequestParam("costs") String costs,
                             HttpSession session, Model model) {
        // 添加订单，清空购物车，添加订单详情，查询订单详细
        model.addAttribute("buyCarList",orderService.findOrderDetail(
                orderService.addOrder(cost, ((User) session.getAttribute("user")).getId(), proIds, quantitys, costs)));
        return "BuyCar_Two";
    }

    /**
     * 确认订单Two
     * @param orderId 订单id
     * @param cost 总价格
     * @return
     */
    @RequestMapping("/confirmAnOrder")
    public String confirmAnOrder(@RequestParam("orderId") Integer orderId,
                                 @RequestParam("cost") Integer cost,Model model) {
        // 通过订单id去查询订单号，// 封装订单号
        model.addAttribute("orderID",orderService.findOrderID(orderId));
        model.addAttribute("cost",cost);
        return "BuyCar_Three";
    }

    /**
     * 支付订单
     * @param orderID 订单号
     * @param cost 总价格
     * @return
     */
    @RequestMapping(value = "/orderPay" , produces = {"text/html;charset=UTF-8"})
    @ResponseBody
    public Object orderPay(@RequestParam("orderID")String orderID,
                           @RequestParam("cost")Integer cost) {
        PaymentBO paymentBO = new PaymentBO();
        paymentBO.setOrderId(orderID);
        paymentBO.setSubject("易买网VIP专属商品");
        paymentBO.setTotal(new BigDecimal(cost));
        return orderService.orderPay(paymentBO);
    }

    /**
     * 去我的订单方法
     */
    @RequestMapping("/toMyOrder")
    public String toMyOrder(Model model, HttpSession session) {
        List<Order> user = orderService.findMyOrder(((User) session.getAttribute("user")).getId());
        user.forEach(System.out::println);
        // 获取当前用户id
        model.addAttribute("orders",orderService.findMyOrder(((User) session.getAttribute("user")).getId()));
        return "Member_Order";

    }


    /**
     * 取消订单
     */
    @RequestMapping("/cancelAnOrder")
    @ResponseBody
    public Boolean cancelAnOrder(Integer orderId) {
        return orderService.deleteOrder(orderId);
    }


}
